<?php

/**
 * twitter actions.
 *
 * @package    twittilicous
 * @subpackage twitter
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */

  
class twitterActions extends sfJSONActions
{

  public function executeIndex()
  {


  }
  
  public function executeAutoFollow()
  {

		$username = "workforoptions";
		$password = "meta11ica02"; 
		// initialize the twitter class
		$twitter = new Twitter($username, $password);
		
		// fetch your profile dms in xml format
		$xml = $twitter->getMessages("xml");
		
		// display the raw xml
		
		//AutoFollow followers
		
		$followers = $twitter->getFollowers('json');
		
		$followersarray = json_decode($followers);
		
		foreach ($followersarray as $follower) {
			   if($twitter->friendshipExists('json','lovedating',''.$follower->screen_name.'') == 'false') {
			   
			   	  //Create a user and twit direct message the user with login details
				    
				 $newUser = new User();
				    $names = explode(' ',$follower->name);
		    	    $newUser->setFirstName($names[0]);
		            $newUser->setLastName($names[1]);
		            $newUser->setNickname($follower->screen_name);
		            $password = substr(md5(rand(100000, 999999)), 0, 6);
		            $newUser->setPassword($password);
		            $newUser->setEmail($follower->id);
		            $newUser->setType('twitter');
		            $newUser->setAvatarId(0);
		            $newUser->save();
			   
			   
		       $twitter->createFriendship('xml',$follower->id);
		       $twitter->newMessage('json',$follower->id, 'Welcome to workforoptions.com, To get started head over to http://www.workforoptions.com or just dm @workforoptions !new your ad.');
		       }
  
  		}
  		
  				return $this->returnJSON($followersarray);

  }
  
  
  public function executeTwitAds()
  {
  
 		$username = "workforoptions";
		$password = "meta11ica02"; 
		
		// initialize the twitter class
		$twitter = new Twitter($username, $password);
		
		// fetch your profile in xml format
		$xml = $twitter->getMessages("xml");
		
		// display the raw xml
		
		//Get direct messages and retwitt them
		
		$directmsgs = $twitter->getMessages('json');
		
		$directmsgsarray = json_decode($directmsgs);
		
		foreach ($directmsgsarray as $msg) {
		
		//Check to see which direct messages are new ads
		
				if (preg_match("/^!new/",$msg->text)) { 
				
					$newAd = new Posts();
					$newAd->setTitle(substr($msg->text, 5, 70).'...');
					$newAd->setDescription(substr($msg->text, 5, 140));
				    $newAd->setPostType('ad');
				    $newAd->save();
				    				    
				    $newCode = new Shortcodes();
					$newCode->setTwitUser($msg->sender->id);
					$newCode->setCode($newAd->getId());
					$newCode->save();
						
				    $newAd->setCodeId($newCode->getId());
				    $newAd->save();

					$twitter->newMessage('json',$msg->sender->id, 'Thank you for your job ad. Your job ad will be reTwitted and posted on workforoptions.com shortly. Ad code:#'.$newAd->getId().'');
				  				  
					$postconfessfortwitter = substr($newAd->getDescription(), 0, 95).'...';
				
				   	$tinyurl = tinyurl::maketinyurl('http://www.workforoptions.com/ads/show/?id='.$newAd->getId().'');
				
					$twitter_status = 'Ad: '.$postconfessfortwitter.' #'.$newCode->getCode().' '.$tinyurl.' #dating #love';
				
					$twitter->updateStatus($twitter_status);
					
					$twitter->destroyMessage('json',$msg->id);
					
				}	
			
		
		 }
		
		return $this->returnJSON($directmsgsarray);
		  
  
    }
    
  public function executeUpdateStatus()
  {
  
  	$username = "workforoptions";
	$password = "meta11ica02"; 
	// initialize the twitter class
	$twitter = new Twitter($username, $password);
		
	// fetch your profile in xml format
		
	$tinyurl = tinyurl::maketinyurl('http://www.twittilicious.com/ads/reply/?id='.$post->getId().'&rid='.$newReply->getId().'');
					
	$twitter->newMessage('json',$twituser, 'Ad Reply:'.$msgtext.' #'.$newReply->getCodeId().' '.$tinyurl.'');			  

		
  
  
  }  
    
  public function executeTwitReply()
  {
  
		$username = "lovedating";
		$password = "meta11ica02"; 
		// initialize the twitter class
		$twitter = new Twitter($username, $password);
		
		// fetch your profile in xml format
		$xml = $twitter->getMessages("xml");
		
		// display the raw xml
		
		//Get direct messages that are replies and send a direct msg to the original author
		
		$directmsgs = $twitter->getMessages('json');
		
		$directmsgsarray = json_decode($directmsgs);
		
		foreach ($directmsgsarray as $msg) {
		
		//Check to see which direct messages are new ads
		
   				 $string = $msg->text;	
				 $match = preg_split('/ /', $string);    
			
				if (count($match) > 1) { 
				
					$postreplyid = substr($match[0],1);
				    $post = PostsPeer::retrieveByPk($postreplyid);  
					$postcode = ShortcodesPeer::retrieveByPk($post->getCodeId());
					$twituser = $postcode->getTwitUser();
					
					$msgtext = preg_replace('#'.$postreplyid.'', '', $msg->text);
					
					
					$newReply = new Posts();
					$newReply->setTitle(substr($msgtext, 0, 70).'...');
					$newReply->setDescription(substr($msgtext, 0, 140));
				    $newReply->setPostType('reply');
				    $newReply->save();
				    				    
				    $newCode = new Shortcodes();
					$newCode->setTwitUser($msg->sender->id);
					$newCode->setCode($newReply->getId());
					$newCode->save();
						
				    $newReply->setCodeId($newCode->getId());
				    $newReply->save();
					
					$tinyurl = tinyurl::maketinyurl('http://www.twittilicious.com/ads/reply/?id='.$post->getId().'&rid='.$newReply->getId().'');
					
					$twitter->newMessage('json',$twituser, 'Ad Reply:'.$msgtext.' #'.$newReply->getCodeId().' '.$tinyurl.'');			  
														
					$twitter->destroyMessage('json',$msg->id);
					
				}	
			
		
		 }
		
		return $this->returnJSON($directmsgsarray);
 
  
    }
    
    public function executeTest() {
    
    $string = '#reply #testing smvsmdvklsmdlkvm';		
	$match = preg_split('/ /', $string); 
	$var = substr($match[0],1);
	echo $var;

	if (count($match) > 1) {   
	echo "<pre>";
    echo $match[0];
     echo "</pre>";
	}
    return $this->returnJSON('hello');

    }

  
  
}
